﻿using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using System.Data;
using System.Text;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class CIController : BaseController
    {
        [Inject]
        public ISMGXJPlanObject SMGXJPlanObject { get; set; }

        [Inject]
        public ISMGXJPlanTicket SMGXJPlanTicket { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        [Inject]
        public ISMGXJPlanObjectTemplate SMGXJPlanObjectTemplate { get; set; }

        protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            ((IBase)SMGXJPlanObject).UserIdentify = UserName;
            ((IBase)SMGXJPlanTicket).UserIdentify = UserName;
            ((IBase)SMGXJPlanObjectTemplate).UserIdentify = UserName;
            base.OnActionExecuting(filterContext);
        }

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)SMGXJPlanObject).Dispose();
            ((IDisposable)SMGXJPlanTicket).Dispose();
            ((IDisposable)SMGXJPlanObjectTemplate).Dispose();
        }

        public ActionResult Server()
        {
            return PartialView();
        }

        /// <summary>
        /// GetAllList
        /// </summary>
        /// <param name="bdId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetObjectTemplateAllList(string TabName)
        {
            try
            {
                var lsitcChr_DeviceName = SMGXJPlanObjectTemplate.GetObjectTemplateList("\'cChr_Classification\' =  \"" + TabName + "\"");
                return Json(new { rows = lsitcChr_DeviceName }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { rows = "" }, JsonRequestBehavior.AllowGet);
            }
        }

        //private HttpContext context = null;
        [HttpPost]
        public JsonResult GetObjectTemplateInfo(string deviceName)
         {        
             try
             {
                 var Templatelsit = SMGXJPlanObjectTemplate.GetObjectTemplateList("\'cChr_DeviceName\' =  \"" + deviceName + "\"");
                 return Json(new { rows = Templatelsit }, JsonRequestBehavior.AllowGet);
             }
             catch (Exception ex)
             {
                 return Json(new { rows = "" }, JsonRequestBehavior.AllowGet);
             }
        }

        //自动补全
        private object GetUnit(HttpContext context)
        {
            context.Response.ContentType = "json";
            string para = context.Request["para"];
            //调用B层方法获取数据
            //EvaluationSystem.BLL.UnitBaseInfoBLL allUnitBll = new UnitBaseInfoBLL();
            var lsit = SMGXJPlanObjectTemplate.GetObjectTemplateList("\'cChr_DeviceName\' =  \"" + context + "\"");
            return View(lsit);
            //DataSet dtUnitInfo = allUnitBll.GetList("");
            //将DataSet转化成Json格式的数据
            //String json = Dataset2JsonCom(dtUnitInfo);
            //context.Response.Write(json);
        }

        #region DataSet转换成Json格式        ///
        /// DataSet转换成Json格式  
        ///   
        /// DataSet 
        ///   
        public static string Dataset2JsonCom(DataSet ds, int total = -1)
        {
            StringBuilder json = new StringBuilder();

            foreach (DataTable dt in ds.Tables)
            {
                //json.Append("[");
                json.Append(DataTable2JsonCom(dt));
                //json.Append("]");
                //json.Append("");
            } return json.ToString();
        }
        #endregion
        #region dataTable转换成Json格式
        ///   
        /// dataTable转换成Json格式  
        ///   
        ///   
        ///   
        public static string DataTable2JsonCom(DataTable dt, int pid = -1)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    int id = pid;
                    jsonBuilder.Append("\"");
                    dt.Columns[j].ColumnName = dt.Columns[j].ColumnName.ToLower();
                    if (dt.Columns[j].ColumnName == "Name") dt.Columns[j].ColumnName = "text";
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    if (dt.Columns[j].ColumnName.ToLower() == "id" && id != -1)
                    {
                        jsonBuilder.Append("\":" + (id * 10 + Convert.ToInt32(dt.Rows[i][j])) + ",");
                    }
                    else
                    {
                        jsonBuilder.Append("\":\"" + dt.Rows[i][j].ToString() + "\",");
                    }
                }

                if (dt.Columns.Count > 0)
                {
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                }
                jsonBuilder.Append("},");
            }
            if (dt.Rows.Count > 0)
            {
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            }
            jsonBuilder.Append("]");
            return jsonBuilder.ToString();
        }
        #endregion dataTable转换成Json格式
    }
}
